package com.devframe.srtp.server.parse;

import java.io.File;
import java.util.List;

import org.jboss.netty.handler.codec.http.multipart.Attribute;
import org.jboss.netty.handler.codec.http.multipart.FileUpload;
import org.jboss.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import org.jboss.netty.handler.codec.http.multipart.InterfaceHttpData;
import org.jboss.netty.handler.codec.http.multipart.InterfaceHttpData.HttpDataType;

import com.devframe.srtp.SRTPConstants;

/**
 * 
 * @author 詹保山
 *解析请求参数
 */
public class SRTPParamParser {
	public static  void parseParams(HttpPostRequestDecoder decoder,SRTPParam requestParams){
		if(decoder==null){
			return ;
		}
		if(requestParams==null){
			requestParams=new SRTPParam();
		}
		try {
			List<InterfaceHttpData> datas=decoder.getBodyHttpDatas();
            if (datas != null) {
                // new value
            	for(InterfaceHttpData data : datas){
            	   if (data.getHttpDataType() == HttpDataType.Attribute){//普通KEY=VALUE
            		   Attribute attribute = (Attribute) data;
            		   String value=attribute.getValue();
            		   String name=attribute.getName();
            		   
            		   if(SRTPConstants.PWD_KEY.equals(name)){//密码的情况下
            			   requestParams.setPwd(value);
            		   }else if(SRTPConstants.USER_NAME_KEY.equals(name)){//用户名
            			   requestParams.setUserName(value);
            		   }else if(SRTPConstants.THUMB_MARK_KEY.equals(name)){//缩略图标记
            			   requestParams.setThumbMark(value);
            		   }else if(SRTPConstants.ACTION_KEY.equals(name)){//动作标识
            			  requestParams.setAction(value); 
            		   }else if(SRTPConstants.FILE_PATH_KEY.equals(name)){//文件路径
            			   requestParams.setFilePath(value);
            		   }else if(SRTPConstants.FILE_NAME_KEY.equals(name)){//文件名称
            			   requestParams.setFileName(value);
            		   }else{
            			   requestParams.putOtherParam(name, value);//其他参数
            		   }
            		}else if(data.getHttpDataType()==HttpDataType.FileUpload){//文件上传
            			 FileUpload fileUpload = (FileUpload) data;
            			  if (fileUpload.isCompleted()) {
            				  requestParams.setFileUpload(fileUpload);
            				  //文件名类型
            				 // requestParams.setFileName(fileUpload.getFilename());
            				  //文件类型
            				  requestParams.setFileContentType(fileUpload.getContentType());
      	                 }
            		}
            	}
            }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

}
